﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MinesweeperModel;

namespace Minesweeper
{
    
    public partial class Form1 : Form
    {
/*        private IMinesweeper ms = new TempMS
            {
                Columns = 3,
                Rows = 3,
                Grid = new Cell[3,3]
                    {
                        {
                            new Cell {NeighborBombCount = 5},
                            new Cell {NeighborBombCount = 9},
                            new Cell {NeighborBombCount = 0}
                        },
                        {
                            new Cell {NeighborBombCount = 5},
                            new Cell {NeighborBombCount = 9},
                            new Cell {NeighborBombCount = 0}
                        },
                        {
                            new Cell {NeighborBombCount = 5},
                            new Cell {NeighborBombCount = 9},
                            new Cell {NeighborBombCount = 0}
                        }
                    }
            };*/

        private Button[,] buttons;
        private IMinesweeper minesweeperModel;
        public Form1(IMinesweeper ms)
        {
            this.minesweeperModel = ms;
            InitializeComponent();

            Setup();
            this.ResumeLayout(false);
            UpdateButtons();

            CreateMenuStrip();
        }

        private void Setup()
        {
            //Buttons
            //
            buttons = new Button[minesweeperModel.Columns,minesweeperModel.Rows];
            for (int x = 0; x < minesweeperModel.Columns; x++)
            {
                for (int y = 0; y < minesweeperModel.Rows; y++)
                {
                    var b = new Button();
                    b.Dock = DockStyle.Fill;
                    b.MouseUp += BOnMouseDown;
                    b.BackgroundImageLayout = ImageLayout.Zoom;

                    buttons[x, y] = b;
                }
            }

            this.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel();
            tableLayoutPanel1.Margin = new Padding(0);
            tableLayoutPanel1.BackColor = Color.Black;

            this.tableLayoutPanel1.SuspendLayout();
            this.SuspendLayout();
            // 
            // tableLayoutPanel1
            // 
            this.tableLayoutPanel1.ColumnCount = minesweeperModel.Columns;

            for (int i = 0; i < minesweeperModel.Columns; i++)
                this.tableLayoutPanel1.ColumnStyles.Add(
                    new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F/minesweeperModel.Columns));

            this.tableLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.tableLayoutPanel1.Name = "tableLayoutPanel1";
            this.tableLayoutPanel1.RowCount = minesweeperModel.Rows;

            for (int i = 0; i < minesweeperModel.Rows; i++)
                this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent,
                                                                                       100F/minesweeperModel.Rows));

            this.tableLayoutPanel1.Size = new System.Drawing.Size(497, 390);
            this.tableLayoutPanel1.TabIndex = 0;

            for (int x = 0; x < minesweeperModel.Columns; x++)
            {
                for (int y = 0; y < minesweeperModel.Rows; y++)
                {
                    tableLayoutPanel1.Controls.Add(buttons[x, y], x, y);
                }
            }


            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(497, 390);
            this.Controls.Add(this.tableLayoutPanel1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.tableLayoutPanel1.ResumeLayout(false);
        }

        private void BOnMouseDown(object sender, MouseEventArgs mouseEventArgs)
        {
            Point p = GetLocation((Button) sender);

            if (mouseEventArgs.Button == System.Windows.Forms.MouseButtons.Left)
            {
                if(!minesweeperModel.Grid[p.X, p.Y].IsFlagged)
                    minesweeperModel.Expose(p.X, p.Y);
            }
            else if (mouseEventArgs.Button == System.Windows.Forms.MouseButtons.Right)
            {
                minesweeperModel.Grid[p.X, p.Y].IsFlagged = !minesweeperModel.Grid[p.X, p.Y].IsFlagged;
            }
            UpdateButtons();
        }



        public void UpdateButtons()
        {
            Cell[,] cells = minesweeperModel.Grid;

            for (int x = 0; x < minesweeperModel.Columns; x++)
            {
                for (int y = 0; y < minesweeperModel.Rows; y++)
                {
                    buttons[x, y].BackgroundImage = Image.FromFile(@"../../BlueSquare.png");
                    buttons[x, y].Margin = new Padding(0);
                    if (cells[x, y].isExposed)
                    {

                        if (cells[x, y].NeighborBombCount == 9)
                        {
                            buttons[x, y].BackgroundImage  = Image.FromFile(@"../../bomb.jpg");
                        }
                        else if (cells[x, y].NeighborBombCount == 0)
                        {
                            buttons[x, y].Text = "";
                            //buttons[x, y].BackgroundImage = null;
                        }
                        else
                        {
                            buttons[x, y].Text = cells[x, y].NeighborBombCount.ToString();
                            //buttons[x, y].BackgroundImage = null;
                        }

                        buttons[x, y].Enabled = false;

                    }
                    else if (cells[x, y].IsFlagged)
                    {
                        buttons[x, y].BackgroundImage = Image.FromFile(@"../../flag.png");
                        
                    }
                    else
                    {
                        //buttons[x, y].BackgroundImage = null;
                    }
                }
            }
        }

        private Point GetLocation(Button b)
        {
            for (int x = 0; x < minesweeperModel.Columns; x++)
            {
                for (int y = 0; y < minesweeperModel.Rows; y++)
                {
                    if(buttons[x, y] == b )
                        return new Point(x,y);
                }
            }
            throw new Exception("Location not found...");
        }

        private void CreateMenuStrip()
        {
            this.menuStrip1 = new System.Windows.Forms.MenuStrip();
            this.fileToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.newToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.easyToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.mediumToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.hardToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.customToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.helpToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.aboutToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.exitToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.menuStrip1.SuspendLayout();
            this.SuspendLayout();
            // 
            // menuStrip1
            // 
            this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.fileToolStripMenuItem,
            this.helpToolStripMenuItem});
            this.menuStrip1.Location = new System.Drawing.Point(0, 0);
            this.menuStrip1.Name = "menuStrip1";
            this.menuStrip1.Size = new System.Drawing.Size(284, 24);
            this.menuStrip1.TabIndex = 0;
            this.menuStrip1.Text = "menuStrip1";
            // 
            // fileToolStripMenuItem
            // 
            this.fileToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.newToolStripMenuItem,
            this.exitToolStripMenuItem});
            this.fileToolStripMenuItem.Name = "fileToolStripMenuItem";
            this.fileToolStripMenuItem.Size = new System.Drawing.Size(37, 20);
            this.fileToolStripMenuItem.Text = "File";

            // 
            // newToolStripMenuItem
            // 
            this.newToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.easyToolStripMenuItem,
            this.mediumToolStripMenuItem,
            this.hardToolStripMenuItem,
            this.customToolStripMenuItem});
            this.newToolStripMenuItem.Name = "newToolStripMenuItem";
            this.newToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
            this.newToolStripMenuItem.Text = "New Game";
           
            // 
            // easyToolStripMenuItem
            // 
            this.easyToolStripMenuItem.Name = "easyToolStripMenuItem";
            this.easyToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
            this.easyToolStripMenuItem.Text = "Easy";
            this.easyToolStripMenuItem.Click += new System.EventHandler(this.EasyToolStripMenuItem_Click);
            // 
            // mediumToolStripMenuItem
            // 
            this.mediumToolStripMenuItem.Name = "mediumToolStripMenuItem";
            this.mediumToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
            this.mediumToolStripMenuItem.Text = "Medium";
            // 
            // hardToolStripMenuItem
            // 
            this.hardToolStripMenuItem.Name = "hardToolStripMenuItem";
            this.hardToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
            this.hardToolStripMenuItem.Text = "Hard";
            // 
            // customToolStripMenuItem
            // 
            this.customToolStripMenuItem.Name = "customToolStripMenuItem";
            this.customToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
            this.customToolStripMenuItem.Text = "Custom...";
            // 
            // helpToolStripMenuItem
            // 
            this.helpToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.aboutToolStripMenuItem});
            this.helpToolStripMenuItem.Name = "helpToolStripMenuItem";
            this.helpToolStripMenuItem.Size = new System.Drawing.Size(44, 20);
            this.helpToolStripMenuItem.Text = "Help";
            // 
            // aboutToolStripMenuItem
            // 
            this.aboutToolStripMenuItem.Name = "aboutToolStripMenuItem";
            this.aboutToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
            this.aboutToolStripMenuItem.Text = "About";
            // 
            // exitToolStripMenuItem
            // 
            this.exitToolStripMenuItem.Name = "exitToolStripMenuItem";
            this.exitToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
            this.exitToolStripMenuItem.Text = "Exit";
            // 
            // Form1
            // 
            this.ClientSize = new System.Drawing.Size(284, 262);
            this.Controls.Add(this.menuStrip1);
            this.MainMenuStrip = this.menuStrip1;
            this.Name = "Form1";
            this.menuStrip1.ResumeLayout(false);
            this.menuStrip1.PerformLayout();
            this.ResumeLayout(false);
            this.PerformLayout();
        }

        private void EasyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            minesweeperModel.Init(MinesweeperModel.Minesweeper.Difficulty.Easy);
            UpdateButtons();
            Setup();
        }
    }

    //internal class TempMS : IMinesweeper
    //{
    //    public int Columns { get; set; }
    //    public int Rows { get; set; }
    //    public int BombCount { get; set; }
    //    public Cell[,] Grid { get; set; }

    //    public void Init()
    //    {
    //        throw new NotImplementedException();
    //    }

    //    public void Flag(int r, int c)
    //    {
    //        throw new NotImplementedException();
    //    }

    //    public void Expose(int r, int c)
    //    {
    //        throw new NotImplementedException();
    //    }

    //    public int BombsRemaining()
    //    {
    //        throw new NotImplementedException();
    //    }

        

    //}
}
